#include "secondwindow.h"
#include<QMenuBar>
#include<QPushButton>
#include<QDebug>
#include<QLineEdit>
#include<QLabel>
#include<QPixmap>
#include<QPainter>
#include<math.h>

secondwindow::secondwindow(QWidget *parent) : QMainWindow(parent)
{
    //setWindowState(Qt::WindowMaximized);
    this->setMinimumSize(800,600);
    this->setWindowIcon(QPixmap(":/new/Camera Roll/XH.jpeg"));
    this->setWindowTitle("分子动力学模拟");

    //说明性文字
//       QLabel * label_a = new QLabel("说明：此分子动力学计算过程为NVE,NVT,NPT系综同时模拟，先进行的是NVE，其次为NVT，最后为NPT。");
//       label_a ->resize(780,80);
//       label_a->move(10,40);
//       label_a->setParent(this);
    // 第二个界面的编辑行

       QLabel * labelc = new QLabel("MD");
       labelc ->resize(150,30);
       labelc ->move(370,60);
       labelc ->setParent(this);
       labelc->show();
       QFont font("Microsoft YaHei",15,75);
       labelc ->setFont(font);


       QLabel * label_a = new QLabel("初始化位置和速度");
       label_a ->resize(150,30);
       label_a ->move(70,250);
       label_a ->setParent(this);

       QLabel * label_a1 = new QLabel("根据设定力场进行演化");
       label_a1->resize(230,30);
       label_a1 ->move(30,360);
       label_a1 ->setParent(this);

       QLabel * label_a2 = new QLabel("根据轨迹数据计算可测物理量");
       label_a2->resize(250,30);
       label_a2 ->move(20,470);
       label_a2 ->setParent(this);


       QLabel * label_b = new QLabel("初始化位置和速度");
       label_b ->resize(150,30);
       label_b ->move(320,250);
       label_b ->setParent(this);

       QLabel * label_b1 = new QLabel("根据设定力场进行演化");
       label_b1->resize(230,30);
       label_b1 ->move(280,360);
       label_b1 ->setParent(this);


       QLabel * label_b2 = new QLabel("根据轨迹数据计算可测物理量");
       label_b2->resize(250,30);
       label_b2 ->move(270,470);
       label_b2 ->setParent(this);

       QLabel * label_c = new QLabel("初始化位置和速度");
       label_c ->resize(150,30);
       label_c ->move(570,250);
       label_c ->setParent(this);

       QLabel * label_c1 = new QLabel("根据设定力场进行演化");
       label_c1->resize(230,30);
       label_c1 ->move(530,360);
       label_c1 ->setParent(this);


       QLabel * label_c2 = new QLabel("根据轨迹数据计算可测物理量");
       label_c2->resize(250,30);
       label_c2 ->move(520,470);
       label_c2 ->setParent(this);

    secondwindowNVE * NVE =new secondwindowNVE;
    secondwindowNVT * NVT =new secondwindowNVT;

    QPushButton * NVEBtn = new QPushButton("NVE");
    NVEBtn->resize(240,60);
    NVEBtn->setParent(this);
    NVEBtn->move(20,110);
    QFont font2("Microsoft YaHei",10,75);
    NVEBtn ->setFont(font2);
    connect(NVEBtn,&QPushButton::clicked,[=](){
        this->hide();
        NVE->show();
    });

    QPushButton * NVTBtn = new QPushButton("NVT");
    NVTBtn->resize(240,60);
    NVTBtn->setParent(this);
    NVTBtn->move(270,110);
    QFont font3("Microsoft YaHei",10,75);
    NVTBtn ->setFont(font3);
    connect(NVTBtn,&QPushButton::clicked,[=](){
        this->hide();
        NVT->show();
    });

    QPushButton * NPTBtn = new QPushButton("NPT");
    NPTBtn->resize(240,60);
    NPTBtn->setParent(this);
    NPTBtn->move(520,110);
    QFont font4("Microsoft YaHei",10,75);
    NPTBtn ->setFont(font4);

//    connect(NVTBtn,&QPushButton::clicked,[=](){
//        this->hide();
//        NVT->show();
//    });


    QPushButton * backBtn = new QPushButton("返回");
    backBtn->resize(50,50);
    backBtn->setParent(this);
    backBtn->move(10,10);
    connect(backBtn,&QPushButton::clicked,[=](){
        qDebug()<<"点击了返回按钮";
        emit this-> secondBack();
    });


    connect(NVE,&secondwindowNVE::NVEBack,this,[=](){
        NVE->hide();
        this->show();
    });

    connect(NVT,&secondwindowNVT::NVTBack,this,[=](){
        NVT->hide();
        this->show();
    });




}
// ----------------------------------------
void secondwindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::black);
    painter.setPen(pen);

    int lineHStartPos_1; // 连接线起点水平位置
    int lineVStartPos_1; // 连接线起点垂直位置
    int lineHEndPos_1;   // 连接线终点水平位置
    int lineVEndPos_1;   // 连接线终点垂直位置

    int lineHStartPos_2;
    int lineVStartPos_2;
    int lineHEndPos_2;
    int lineVEndPos_2;

    int lineHStartPos_3;
    int lineVStartPos_3;
    int lineHEndPos_3;
    int lineVEndPos_3;

    int lineHStartPos_4;
    int lineVStartPos_4;
    int lineHEndPos_4;
    int lineVEndPos_4;

    int lineHStartPos_5;
    int lineVStartPos_5;
    int lineHEndPos_5;
    int lineVEndPos_5;

    int lineHStartPos_6;
    int lineVStartPos_6;
    int lineHEndPos_6;
    int lineVEndPos_6;


    int lineHStartPos_7;
    int lineVStartPos_7;
    int lineHEndPos_7;
    int lineVEndPos_7;

    int lineHStartPos_8;
    int lineVStartPos_8;
    int lineHEndPos_8;
    int lineVEndPos_8;

    int lineHStartPos_9;
    int lineVStartPos_9;
    int lineHEndPos_9;
    int lineVEndPos_9;



    lineHStartPos_1 = 140;
    lineVStartPos_1 = 170;
    lineHEndPos_1 = 140;
    lineVEndPos_1 = 250;

    lineHStartPos_2 = 390;
    lineVStartPos_2 = 170;
    lineHEndPos_2 = 390;
    lineVEndPos_2 = 250;

    lineHStartPos_3 = 640;
    lineVStartPos_3 = 170;
    lineHEndPos_3 = 640;
    lineVEndPos_3 = 250;

    lineHStartPos_4 = 140;
    lineVStartPos_4 = 280;
    lineHEndPos_4 = 140;
    lineVEndPos_4 = 360;

    lineHStartPos_5 = 390;
    lineVStartPos_5 = 280;
    lineHEndPos_5 = 390;
    lineVEndPos_5 = 360;

    lineHStartPos_6 = 640;
    lineVStartPos_6 = 280;
    lineHEndPos_6 = 640;
    lineVEndPos_6 = 360;

    lineHStartPos_7 = 140;
    lineVStartPos_7 = 390;
    lineHEndPos_7 = 140;
    lineVEndPos_7 = 470;

    lineHStartPos_8 = 390;
    lineVStartPos_8 = 390;
    lineHEndPos_8 = 390;
    lineVEndPos_8 = 470;

    lineHStartPos_9 = 640;
    lineVStartPos_9 = 390;
    lineHEndPos_9 = 640;
    lineVEndPos_9 = 470;

    QLineF line_1(lineHStartPos_1, lineVStartPos_1, lineHEndPos_1, lineVEndPos_1);
    painter.drawLine(line_1);
    QLineF line_2(lineHStartPos_2, lineVStartPos_2, lineHEndPos_2, lineVEndPos_2);
    painter.drawLine(line_2);
    QLineF line_3(lineHStartPos_3, lineVStartPos_3, lineHEndPos_3, lineVEndPos_3);
    painter.drawLine(line_3);
    QLineF line_4(lineHStartPos_4, lineVStartPos_4, lineHEndPos_4, lineVEndPos_4);
    painter.drawLine(line_4);
    QLineF line_5(lineHStartPos_5, lineVStartPos_5, lineHEndPos_5, lineVEndPos_5);
    painter.drawLine(line_5);
    QLineF line_6(lineHStartPos_6, lineVStartPos_6, lineHEndPos_6, lineVEndPos_6);
    painter.drawLine(line_6);
    QLineF line_7(lineHStartPos_7, lineVStartPos_7, lineHEndPos_7, lineVEndPos_7);
    painter.drawLine(line_7);
    QLineF line_8(lineHStartPos_8, lineVStartPos_8, lineHEndPos_8, lineVEndPos_8);
    painter.drawLine(line_8);
    QLineF line_9(lineHStartPos_9, lineVStartPos_9, lineHEndPos_9, lineVEndPos_9);
    painter.drawLine(line_9);




    // 箭头的两点坐标
    double x1, y1, x2, y2;

    // 求得箭头两点坐标
    CalcVertexes(lineHStartPos_1, lineVStartPos_1, lineHEndPos_1, lineVEndPos_1, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_1, lineVEndPos_1, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_1, lineVEndPos_1, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_2, lineVStartPos_2, lineHEndPos_2, lineVEndPos_2, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_2, lineVEndPos_2, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_2, lineVEndPos_2, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_3, lineVStartPos_3, lineHEndPos_3, lineVEndPos_3, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_3, lineVEndPos_3, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_3, lineVEndPos_3, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_4, lineVStartPos_4, lineHEndPos_4, lineVEndPos_4, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_4, lineVEndPos_4, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_4, lineVEndPos_4, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_5, lineVStartPos_5, lineHEndPos_5, lineVEndPos_5, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_5, lineVEndPos_5, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_5, lineVEndPos_5, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_6, lineVStartPos_6, lineHEndPos_6, lineVEndPos_6, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_6, lineVEndPos_6, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_6, lineVEndPos_6, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_7, lineVStartPos_7, lineHEndPos_7, lineVEndPos_7, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_7, lineVEndPos_7, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_7, lineVEndPos_7, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_8, lineVStartPos_8, lineHEndPos_8, lineVEndPos_8, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_8, lineVEndPos_8, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_8, lineVEndPos_8, x2, y2); // 绘制箭头另一半

    CalcVertexes(lineHStartPos_9, lineVStartPos_9, lineHEndPos_9, lineVEndPos_9, x1, y1, x2, y2);
    painter.drawLine(lineHEndPos_9, lineVEndPos_9, x1, y1); // 绘制箭头一半
    painter.drawLine(lineHEndPos_9, lineVEndPos_9, x2, y2); // 绘制箭头另一半
}

// ---------------------------------------------------------------------------------------------------------------------------------------
void secondwindow::CalcVertexes(double startX, double startY, double endX, double endY, double& x1, double& y1, double& x2, double& y2)
{
    /*
     * @brief 求得箭头两点坐标
     */

    double arrowLength = 10;      // 箭头长度，一般固定
    double arrowDegrees = 0.5;    // 箭头角度，一般固定

    // 求 y / x 的反正切值
    double angle = atan2(endY - startY, endX - startX) + 3.1415926;

    // 求得箭头点 1 的坐标
    x1 = endX + arrowLength * cos(angle - arrowDegrees);
    y1 = endY + arrowLength * sin(angle - arrowDegrees);

    // 求得箭头点 2 的坐标
    x2 = endX + arrowLength * cos(angle + arrowDegrees);
    y2 = endY + arrowLength * sin(angle + arrowDegrees);

}

//void secondwindow::paintEvent(QPaintEvent *)
//    {
//        QPainter painter(this);
//        QPixmap pix;
//        pix.load(":/new/Camera Roll/MD.jpg");
//        painter.drawPixmap(0,0,this->width(),this->height(),pix);
//}
